package servlets;

import java.io.*;
import java.sql.Connection;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

import dataAccess.ConnectionDB;
import dataAccess.ContactoDAO;
import bussinesObjetcs.ContactoBean;




/** Servlet that reads a customer ID and displays
 *  information on the account balance of the customer
 *  who has that ID.
 *  <p>
 *  From <a href="http://courses.coreservlets.com/Course-Materials/">the
 *  coreservlets.com tutorials on servlets, JSP, Struts, JSF, Ajax, GWT, 
 *  Spring, Hibernate/JPA, and Java programming</a>.
 */

@WebServlet("/AddServletContacto")
public class AddServletContacto extends HttpServlet {


/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

@Override

public void doPost (HttpServletRequest request,
		            HttpServletResponse response)
	  throws ServletException, IOException 
{ 
doGet(request, response); 
}
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
	// pide los datos de la forma con el get paramater con request
	String nombrecompleto = request.getParameter("nombrecompleto");
	String email = request.getParameter("email");
	String asunto = request.getParameter("asunto");
	String sugerencia = request.getParameter("sugerencia");

	//crear el bean y guarda los datos en el Bean
	ContactoBean contacto = new ContactoBean();
	
	contacto.setNombrecompleto(nombrecompleto);
	contacto.setEmail(email);
	contacto.setAsunto(asunto);
	contacto.setSugerencia(sugerencia);

	//Guardar instacia en base de datos
	ConnectionDB connectionDB = new ConnectionDB();
	Connection connection = connectionDB.getConnectionDB();
	
	ContactoDAO contactoDao = new ContactoDAO(connection);
	contactoDao.addUser(contacto);
	  
//    String customerId = request.getParameter("customerId");
//    CustomerLookupService service = new CustomerSimpleMap();
//    Customer customer = service.findCustomer(customerId);  // OK to pass null or empty String to findCustomer
    //guarada un atributo en memoria para poder leer en otra pagina
	request.setAttribute("contacto", contacto);
    RequestDispatcher dispatcher= request.getRequestDispatcher("displayContacto.jsp");
    dispatcher.forward(request, response);
    
   // protected void doPost()
  }
}